﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using JF.RealtimeNotifications.Notifiers.Realtime;
using JF.RealtimeNotifications.Notifiers;
using System.Threading;
using JF.RealtimeNotifications.Notifiers.Extensibility;
using JF.RealtimeNotifications.Notifiers.Growl;
using JF.RealtimeNotifications.Notifiers.Feeds;
using System.Runtime.Serialization.Json;
using System.IO;
using System.Runtime.Serialization;

namespace JF.RealtimeNotifications.Test.Console
{
    class Program
    {
        [DataContract]
        class SubscribedAnnouncement
        {
            [DataMember(Name="cm")]
            public String ConnectionMetadata { get; set; }
            [DataMember(Name = "ch")]
            public String Channel { get; set; }
        }

        class FeedTriggerMessage
        {
            public String Title { get; private set; }
            public String Description { get; private set; }
            public String Link { get; private set; }
            public DateTime Published { get; private set; }

            public FeedTriggerMessage(String title,String description,String link, DateTime published)
            {
                Title = title;
                Description = description.Length > 100 ? description.Substring(0,97) + "..." : description;
                Link = link;
                Published = published;
            }

            public override string ToString()
            {
                return "{\"xrtml\":{\"t\":\"newfeed\",\"a\":\"insert\",\"d\":{\"content\":{\"title\":\"" + Title + "\",\"description\":\"" + Description + "\",\"link\":\"" + Link + "\",\"published\":\"" + Published + "\"}}}}";
            }
        }

        static void Main(string[] args)
        {            
            var realtimeNotifier = NotifierFactory.CreateNotifier<RealtimeNotifier>();
            var growlNotifier = CreateGrowlNotifier();

            growlNotifier.Connect(null);

            realtimeNotifier.Connect((error) =>
            {
                realtimeNotifier.Subscribe(new Notification("ortcClientSubscribed", String.Empty), (notification) =>
                {
                    DataContractJsonSerializer serializer = new DataContractJsonSerializer(typeof(SubscribedAnnouncement));
                    SubscribedAnnouncement announcementData = (SubscribedAnnouncement)serializer.ReadObject(new MemoryStream(Encoding.UTF8.GetBytes(notification.Text)));

                    growlNotifier.Publish(new RealtimeNotification(
                            "Channel Subscribed",
                            String.Format("{0} - {1}",announcementData.Channel,announcementData.ConnectionMetadata)
                        ));
                });
            });

            CreateFeedsNotifier((notification) => {
                var feed = notification as FeedNotification;
                growlNotifier.Publish(new FeedNotification("New Feed",feed.Item));
                
                var feedTrigger = new FeedTriggerMessage(feed.Item.Title,feed.Item.Description,feed.Item.Link,feed.Item.PublishDate);
                realtimeNotifier.Publish(new RealtimeNotification("feeds", feedTrigger.ToString()));
            });

            System.Console.Read();
        }

        static GrowlNotifier CreateGrowlNotifier()
        {
            var growlNotifier = NotifierFactory.CreateNotifier<GrowlNotifier>();

            growlNotifier.ApplicationName = "Codeplex";
            growlNotifier.Notifications.Add(new RealtimeNotification("Client Connected",String.Empty));
            growlNotifier.Notifications.Add(new FeedNotification("New Feed",null));

            return growlNotifier;
        }

        static FeedsNotifier CreateFeedsNotifier(OnNotificationDelegate onFeedNotification)
        {
            var feedsNotifier = NotifierFactory.CreateNotifier<FeedsNotifier>();

            feedsNotifier.FeedPollingTime = 10;

            feedsNotifier.Connect(null);

            feedsNotifier.Subscribe(new Notification("ReleaseLog", "http://feeds.feedburner.com/releaselog"), onFeedNotification);
            feedsNotifier.Subscribe(new Notification("HowToGeek", "http://feeds.howtogeek.com/HowToGeek"), onFeedNotification);        
            //feedsNotifier.Subscribe(new Notification("Blog", "http://howdoyouphil.blogspot.com/feeds/posts/default"), onFeedNotification);
            
            return feedsNotifier;
        }   
    }
}
